#ifndef _DISPLAY_HPP_
#define _DISPLAY_HPP_

#include "main.h"
#include "style.h"
#include "item.hpp"
#include "actor.hpp"


// 块 : 左上点坐标(x,y), 尺寸(w,h), 背景色(bgcolor), 文本内容(text), 结果块值(value)
class Block
{
private:
public:
    int x, y, w, h;
    string title;
    string text;
    string value;
    string bgcolor;
    string color = s_white;

public:
    Block(int x, int y, int w, int h)
    {
        this->x = x;
        this->y = y;
        this->w = w;
        this->h = h;
    }
    // 背景色
    string paintbg()
    {
        string bg = bgcolor;
        for (int i = 0; i < h; i++)
        {
            bg += s_moveto(x, y + i) + string(w, ' ');
        }
        bg += s_end;
        return bg;
    }
    // 刷新显示
    string refresh()
    {
        value = paintbg() + color + s_moveto(x, y) + bgcolor + title + text + s_end;
        return value;
    }

};

// 消息框
class MsgBox : public Block
{
protected:
    string msgs[20] = {};
    int last = 0;

public:
    MsgBox(int x, int y, int w, int h) : Block(x, y, w, h) {}

    void push(string s)
    {
        text = "";
        msgs[last] = s;

        for (int i = 1; i < 20; i++)
        {
            // text += s_moveto(x, y+j) + msgs[i];
            text += s_moveto(x, y + i) + msgs[(last + i + 1) % 20];
        }
        last++;
        if (last == 20)
            last = 0;
    }

};

// 输入框
class InputBox : public Block
{
public:
    string func;
    int idx = 0;
    string op;
    double amount = 0.0;

    InputBox(int x, int y, int w, int h) : Block(x, y, w, h) {}

    void input()
    {
        cout << paintbg() + s_moveto(x, y);
        cin >> func >> idx;
    }
};

// 角色列表
class ActorsBox : public Block
{

public:
    Actor* a[2] = {nullptr,nullptr};
    int cap = 2;

    ActorsBox(int x, int y, int w, int h) : Block(x, y, w, h) {}
    // 向列表中添加角色
    void add(Actor* act)
    {
        text = "";
        for (int i = 0; i < cap; i++)
        {
            if (a[i]->hide)
            {
                act->hide = false;
                a[i] = act;
                update();
                return;
            }
        }
        text = "[ERR] actor list is full.";
    }

    // 更新列表内容
    void update()
    {
        text = "";
        for (int i = 0; i < cap; i++)
        {
            if (!a[i]->hide)
            {
                text += s_moveto(x, y + i + 1) + to_string(i) + " " + a[i]->name;
            }
        }
    }
    // 从列表中移除角色
    void remove(int i)
    {
        if (i < cap)
        {
            a[i]->hide = true;
            update();
            return;
        }
        text = "[ERR] The index out of range.";
    }
};

// 角色信息
class InfoBox : public Block
{
    public:
    Actor player;

    InfoBox(int x, int y, int w, int h, Actor a) : Block(x, y, w, h) {
        player = a;
    }

    void update(){
        text = s_moveto(x, y +  1) + " H P : " + to_string(player.hp);
    }
};
// 物品信息
class ItemBox : public Block{
public:
    int cap = 20;
    // 物品栏列表
    Item *itms = new Item[cap];

    ItemBox(int x, int y, int w, int h) : Block(x, y, w, h) {}
    // 向列表中添加物品
    void add(Item itm)
    {
        text = "";
        for (int i = 0; i < cap; i++)
        {
            if (itms[i].hide)
            {
                itm.hide = false;
                itms[i] = itm;
                update();
                return;
            }
        }
        text = "[ERR] actor list is full.";
    }
    // 更新列表内容
    void update()
    {
        text = "";
        for (int i = 0; i < cap; i++)
        {
            if (!itms[i].hide)
            {
                text += s_moveto(x + i%10, y + i/10 + 1) + to_string(i) + " " + itms[i].name;
            }
        }
    }
    // 从列表中移除角色
    void remove(int i)
    {
        if (i < cap)
        {
            itms[i].hide = true;
            update();
            return;
        }
        text = "[ERR] The index out of range.";
    }
};

// 物品信息栏
class ItemInfoBox : public Block
{
    public:
    Item itm;

    ItemInfoBox(int x, int y, int w, int h) : Block(x, y, w, h) {}
    void update(){
        text = "";
        for(int i = 0; i < 12; i++){
            text += s_moveto(x,y+i+1) + "名称: " + itm.name;
        }
    }
};

// 角色装备信息
class EqptInfoBox : public Block
{
    public:
    Actor act;

    EqptInfoBox(int x, int y, int w, int h) : Block(x, y, w, h) {}
    void update(){
        text = "";
        // 防具
        // text += s_moveto(x,y+1) + "帽子: " + actor.head.name \
        //       + s_moveto(x,y+2) + "护甲: " + actor.body.name \
        //       + s_moveto(x,y+3) + "护手: " + actor.hand.name \
        //       + s_moveto(x,y+4) + "护腿: " + actor.leg.name \
        //       + s_moveto(x,y+5) + "鞋子: " + actor.foot.name ;
        // // 武器
        //text += s_moveto(x,y+6) + "左手: " + actor.left.name; 
        text  += s_moveto(x,y+7) + "右手: " + act.right_hand.name; 
        //       + s_moveto(x,y+8) + "副手: " + actor.deputy.name ;
        // // 饰品
        // text += s_moveto(x,y+9) + "头饰: " + actor.topknot.name \
        //       + s_moveto(x,y+10) + "项链: " + actor.neck.name \
        //       + s_moveto(x,y+11) + "手镯: " + actor.bracelet.name \
        //       + s_moveto(x,y+12) + "戒指: " + actor.ring.name ;
    }
};

#endif